﻿using System.Linq;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Nxg.Blog.Core;
using Nxg.Blog.Core.Documents;

namespace Nxg.Blog.Web.Controllers {
    [Authorize (AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
    public class SecureController : BaseController {
        protected readonly IViewProjectionFactory _viewProjectionFactory;

        public SecureController (IViewProjectionFactory viewProjectionFactory) {
            _viewProjectionFactory = viewProjectionFactory;
        }

        public override void OnActionExecuting (ActionExecutingContext context) {
            base.OnActionExecuting (context);
            ViewBag.Author = CurrentUser;
        }
        public Author CurrentUser {
            get {
                var authorId = HttpContext.User.Claims.FirstOrDefault (x => x.Type == "AuthorId").Value;
                var author = _viewProjectionFactory.Get<int, Author> (int.Parse (authorId));
                return author;
            }
        }
    }
}